package View;

import Controller.ActionManager;
import Model.Remote;
import catchup.Main;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

/**
 *
 * @author pieterc
 */
public class MainFrame extends JFrame {

    private MessagePanelContainer messages;
    private JScrollPane sp;
    private Remote remote;

    public JScrollPane getSp() {
        return sp;
    }

    public MainFrame() {
        super("Catch Up " + Main.version);
        try {
            this.setIconImage((new ImageIcon("catchup.png")).getImage());
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                System.err.println("Error: cannot instantiate look and feel");
            }
            File f = new File("KeyStore.properties");
            if (!f.exists()) {

            }
            JPanel cp = new JPanel();
            messages = new MessagePanelContainer();
            sp = new JScrollPane(messages);
            sp.getVerticalScrollBar().setUnitIncrement(8);
            sp.getVerticalScrollBar().setBlockIncrement(8);
            remote = new Remote(messages);
            JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, sp, new PostPanel());
            this.setContentPane(jsp);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jsp.setDividerLocation(450);
            this.setPreferredSize(new Dimension(450, 550));
            JMenuBar jmb = new JMenuBar();
            JMenu jm = new JMenu("File");
            JMenuItem jmi1 = new JMenuItem(ActionManager.getRefreshAction());
            JMenuItem jmi2 = new JMenuItem("Close");
            jmi2.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            jm.add(jmi1);
            jm.add(jmi2);
            jmb.add(jm);
            JMenu edit = new JMenu("Edit");
            JMenuItem jmi3 = new JMenuItem(ActionManager.getDeleteAction());
            JMenuItem jmi4 = new JMenuItem(ActionManager.getFaveAction());
            JMenuItem jmi5 = new JMenuItem(ActionManager.getRepeatAction());
            edit.add(jmi3);
            edit.add(jmi4);
            edit.add(jmi5);
            edit.add(new JSeparator(JSeparator.HORIZONTAL));
            edit.add(new JMenuItem(ActionManager.getConfigureAccountsAction()));
            jmb.add(edit);
            JMenu jm3 = new JMenu("Tools");
            jm3.add(new JMenuItem("Configure Plugins"));
            jmb.add(jm3);
            JMenu help = new JMenu("Help");
            help.add(new JMenuItem("Go to online user documentation"));
            help.add(new JMenuItem("Go to online catchup-developer documentation"));
            help.add(new JMenuItem("Go to online plugin-developer documentation"));
            help.add(new JMenuItem("Get Source"));
            jmb.add(help);
            this.setJMenuBar(jmb);
            this.pack();

            this.addWindowListener(new WindowListener(){

                public void windowOpened(WindowEvent e) {
                }

                public void windowClosing(WindowEvent e) {
                    try {
                        ActionManager.getRemote().store();
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                public void windowClosed(WindowEvent e) {
                }

                public void windowIconified(WindowEvent e) {
                }

                public void windowDeiconified(WindowEvent e) {
                }

                public void windowActivated(WindowEvent e) {
                }

                public void windowDeactivated(WindowEvent e) {
                }
            });

        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
